using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Skill : MonoBehaviour
{
    [SerializeField] protected float coolDown;
    protected GameTimer coolDownTimer;

    protected Player player;

    protected virtual void Awake()
    { 
        coolDownTimer = new GameTimer();
    }

    protected virtual void Start()
    {
        player = PlayerManager.instance.player;
    }

    protected virtual void Update()
    { 
        coolDownTimer.Timer -= Time.deltaTime; 
    }

    public virtual bool CanUseSkill()
    {
        if (player.stateMachine.currentState == player.ultimateSkillState)
        {
            return false;
        }
        if (coolDownTimer.isOver())
        {
            coolDownTimer.Timer = coolDown;
            return true;
        }
        return false;
    }

    public virtual void UseSkill()
    { 
    
    }

    public GameObject FindClosestEnmey(Transform tf,float checkRadius = 10)
    {
        Collider2D[] cols = Physics2D.OverlapCircleAll(tf.position, checkRadius,player.whatIsEnemy);
        GameObject closestEnemy = null;
        float distance = Mathf.Infinity;
        foreach (Collider2D hit in cols)
        {
            if (hit.GetComponent<Enemy>() != null)
            {
                float tD = Vector2.Distance(hit.gameObject.transform.position, tf.position);
                if (tD < distance)
                {
                    distance = tD;
                    closestEnemy = hit.gameObject;
                }
            }
        }

        return closestEnemy;


    }

    public virtual void StudySkill()
    { 
    
    }

    public virtual float GetCooldownRate()
    {
        if (coolDown == 0) return 0;
        return coolDownTimer.Timer / coolDown;
    }
}
